﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 废料招标单价 : System.Web.UI.Page
    {
        string sql = "select * from 废料季度招标单价 inner join 废料 on 废料季度招标单价.废料ID=废料.ID ";
        protected void Page_Load(object sender, EventArgs e)
        {
            int bid = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out bid);
                }               
            }
            this.SqlDataSource1.SelectCommand = sql + "order by 年度 desc,季度 desc ";
            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共有 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            decimal jihuadanjia;          
            decimal shijidanjia;

            if (!decimal.TryParse(this.JiHuaDanJia.Text, out jihuadanjia))
            {
                this.Message.Text = "“计划单价”录入有误";
                return;
            }
            else if (jihuadanjia <= 0)
            {
                this.Message.Text = "“计划单价”必须大于 0";
                return;
            }
            if (!decimal.TryParse(this.ShiJiDanJia.Text, out shijidanjia))
            {
                this.Message.Text = "“实际单价”录入有误";
                return;
            }
            else if (shijidanjia <= 0)
            {
                this.Message.Text = "“实际单价”必须大于 0";
                return;
            }
            this.SqlDataSource1.InsertParameters["年度"].DefaultValue = this.NianDu.Text;
            this.SqlDataSource1.InsertParameters["季度"].DefaultValue = this.JiDu.Text;
            this.SqlDataSource1.InsertParameters["废料ID"].DefaultValue = this.FeiLiao.Text;
            this.SqlDataSource1.InsertParameters["计划单价"].DefaultValue = jihuadanjia.ToString();
            this.SqlDataSource1.InsertParameters["实际单价"].DefaultValue = shijidanjia.ToString();
            try
            {
                if (this.SqlDataSource1.Insert() > 0)
                {
                    this.Message.Text = "增加成功";
                }
            }
            catch (Exception)
            {
                this.Message.Text = "该季度废料单价可能已经存在！";
                return;
            }
            this.JiHuaDanJia.Text = "";
            this.ShiJiDanJia.Text = "";
           

        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string sql0=" where 0=0 ";
            if (this.Year.Text != "全部")
            {
                sql0 += " and 年度=" + this.Year.Text;
            }
            if (this.Quarter.Text != "0")
            {
                sql0 += " and 季度=" + this.Quarter.Text;
            }
            if (this.FeiLiao0.Text != "0")
            {
                sql0 += " and 废料ID=" + this.FeiLiao0.Text;
            }
            this.SqlDataSource1.SelectCommand = sql + sql0;
        }

        protected void Add_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = true;
            this.Panel3.Visible = false;
        }

        protected void Button_Cancle_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
        }
    }
}
